﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Firefly.Framework.Windows
{
    public class StretchThemePart : ThemePart
    {
        public Dictionary<string, List<string>> PartLists { get; set; }

        public StretchThemePart()
        {
            PartLists = new Dictionary<string, List<string>>();
        }

        public override void Draw(string style, Rectangle dest, Color color)
        {
            Rectangle[] destRects = new Rectangle[9];
            List<string> parts;

            if (PartLists.ContainsKey(style))
            { 
                parts = PartLists[style]; 
            }
            else
            { 
                parts = PartLists["Default"]; 
            }

            int X1;
            int X2;
            int X3;
            int Y1;
            int Y2;
            int Y3;
            int W1;
            int W2;
            int W3;
            int H1;
            int H2;
            int H3;
            W1 = Rectangles[parts[0]].Width;
            W3 = Rectangles[parts[2]].Width;
            W2 = dest.Width - (W1 + W3);
            H1 = Rectangles[parts[0]].Height;
            H3 = Rectangles[parts[6]].Height;
            H2 = dest.Height - (H1 + H3);
            X1 = dest.X;
            X2 = X1 + W1;
            X3 = X2 + W2;
            Y1 = dest.Y;
            Y2 = Y1 + H1;
            Y3 = Y2 + H2;
            destRects[0] = new Rectangle(X1, Y1, W1, H1);
            destRects[1] = new Rectangle(X2, Y1, W2, H1);
            destRects[2] = new Rectangle(X3, Y1, W3, H1);
            destRects[3] = new Rectangle(X1, Y2, W1, H2);
            destRects[4] = new Rectangle(X2, Y2, W2, H2);
            destRects[5] = new Rectangle(X3, Y2, W3, H2);
            destRects[6] = new Rectangle(X1, Y3, W1, H3);
            destRects[7] = new Rectangle(X2, Y3, W2, H3);
            destRects[8] = new Rectangle(X3, Y3, W3, H3);

            for (int i = 0; i < 9; i++)
            {
                WindowsService.Instance.SpriteBatch.Draw(Texture, destRects[i], Rectangles[parts[i]], color);
            }
        }

        private void CheckPartLists()
        {
            foreach (KeyValuePair<string, List<string>> style in this.PartLists)
            {
                if (style.Value.Count != 9)
                {
                    throw new ApplicationException("The style '" + style.Key + "' does not have 9 parts.");
                }

                foreach (string key in style.Value)
                {
                    if (!Rectangles.ContainsKey(key))
                    {
                        throw new ApplicationException("The style '" + style.Key + "' uses a rectangle with the key '" + key + "' which is undefined.");
                    }
                }
            }
        }

        #region IPipelineSerializable Members

        public override void DeserializeContent(ContentReader input)
        {
            base.DeserializeContent(input);
            PartLists = input.ReadObject<Dictionary<string, List<string>>>();

            if (!PartLists.ContainsKey("Default"))
            { 
                throw new ApplicationException("A Default PartList must be specified."); 
            }

            CheckPartLists();
        }

        #endregion

        #region IDisposable Members

        ~StretchThemePart()
        {
            Dispose(false);
        }

        #endregion
    }
}
